<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace common\traits;

use yii\web\NotFoundHttpException;

/**
 * Description of Constants
 *
 * @author emhome
 */
trait QueryInstanceActiveRecord {

    /**
     * findOrNew
     * Get member portlet panel.
     * @return \yii\db\ActiveRecord
     */
    public static function findOrNew($condition) {
        $model = static::findOne($condition);
        if ($model == null) {
            $className = self::className();
            $model = new $className();
            if (is_array($condition)) {
                $model->setAttributes($condition);
            }
        }
        return $model;
    }

    /**
     * findOrNew
     * Get member portlet panel.
     * @return string
     */
    public static function findOrFail($condition) {
        $model = static::findOne($condition);
        if ($model == null) {
            throw new NotFoundHttpException('未找到数据模型对应的记录！');
        }
        return $model;
    }

}
